<?php
/*
 * @file livedata.dashboard.inc
 * Config forms for this module
 *
 */
 
/**
 * Form builder
 */
function livedata_dashboard_form($form, &$form_state){
    
  $form = livedata_dashboard_form_sunbox_options($form);
	$form = livedata_dashboard_form_moonbox_options($form);

  $form['submit'] = array(
      '#type'	 => 'submit',
      '#value' => t('Save')
  );

  return $form;
}


/**
 * save the settings from the functions and the services from the uissync dashboard 
 */
function livedata_dashboard_form_submit($form, &$form_state){
  
		variable_set('livedata_sunbox_picture_enabled', $form_state['values']['sunbox']['picture_enabled']);
		variable_set('livedata_sunbox_picture_style', $form_state['values']['sunbox']['picture_style']);
  	variable_set('livedata_sunbox_picture_url', $form_state['values']['sunbox']['picture_url']);
		
		variable_set('livedata_moonbox_picture_enabled', $form_state['values']['moonbox']['picture_enabled']);
		variable_set('livedata_moonbox_picture_style', $form_state['values']['moonbox']['picture_style']);
		variable_set('livedata_moonbox_illumination', $form_state['values']['moonbox']['phase_settings']['illumination']);
		variable_set('livedata_moonbox_nnm', $form_state['values']['moonbox']['phase_settings']['next_new_moon']);
		variable_set('livedata_moonbox_nfq', $form_state['values']['moonbox']['phase_settings']['next_first_quarter']);
		variable_set('livedata_moonbox_nfm', $form_state['values']['moonbox']['phase_settings']['next_full_moon']);
		variable_set('livedata_moonbox_nlq', $form_state['values']['moonbox']['phase_settings']['next_last_quarter']);
    
    drupal_set_message(t('Settings are saved'));
}

/**
 * add the functions fieldset with checkboxes
 */
function livedata_dashboard_form_sunbox_options($form){
    
	$styles = image_styles();
		foreach($styles as $name => $value)
		{
			$style_names[] = $name;	
		}
		
    // create the group container
    $form['sunbox'] = array(
	'#type' => 'fieldset',
	'#title' => t('Sunbox'),
	'#collapsible' => true,
	'#collapsed' => false,
  '#tree' => true
    );
    
 
    $form['sunbox']['picture_enabled'] = array(
          '#type' => 'checkbox',
		      '#title' => t('Picture Enabled'),
          '#default_value' => variable_get('livedata_sunbox_picture_enabled', TRUE)
					);
					
		$form['sunbox']['picture_style'] = array(
					'#type' => 'select',
					'#title' => t('Image style'),
					'#options' => $style_names,
					'#default_value' => variable_get('livedata_sunbox_picture_style')
					);
		
					
    $form['sunbox']['picture_url'] = array(
          '#type' => 'textfield',
		      '#title' => t('Picture Url'),
          '#default_value' => variable_get('livedata_sunbox_picture_url', 'http://sdo.gsfc.nasa.gov/assets/img/latest/latest_512_HMIIC.jpg'),
					'#required' => TRUE
					);
	
    
    return $form;
}

function livedata_dashboard_form_moonbox_options($form){
    
		$styles = image_styles();
		foreach($styles as $name => $value)
		{
			$style_names[] = $name;	
		}
		
    // create the group container
    $form['moonbox'] = array(
	'#type' => 'fieldset',
	'#title' => t('Moonbox'),
	'#collapsible' => true,
	'#collapsed' => false,
  '#tree' => true
    );
    
    $form['moonbox']['picture_enabled'] = array(
    '#type' => 'checkbox',
		'#title' => t('Picture Enabled'),
    '#default_value' => variable_get('livedata_moonbox_picture_enabled', TRUE)
					);
		
		$form['moonbox']['picture_style'] = array(
			'#type' => 'select',
			'#title' => t('Image style'),
			'#options' => $style_names,
			'#default_value' => variable_get('livedata_moonbox_picture_style')
					);
					
		$form['moonbox']['phase_settings'] = array(
    '#type' => 'fieldset',
		'#title' => t('Phase settings'),
    '#collapsible' => false
					);
					
				$form['moonbox']['phase_settings']['illumination'] = array(
    		'#type' => 'checkbox',
				'#title' => t('Illumination'),
    		'#default_value' => variable_get('livedata_moonbox_illumination', TRUE)
					);
					
				$form['moonbox']['phase_settings']['next_full_moon'] = array(
    		'#type' => 'checkbox',
				'#title' => t('Next full moon'),
    		'#default_value' => variable_get('livedata_moonbox_nfm', TRUE)
					);

				$form['moonbox']['phase_settings']['next_new_moon'] = array(
    		'#type' => 'checkbox',
				'#title' => t('Next new moon'),
    		'#default_value' => variable_get('livedata_moonbox_nnm', TRUE)
					);

				$form['moonbox']['phase_settings']['next_first_quarter'] = array(
    		'#type' => 'checkbox',
				'#title' => t('Next first quarter'),
    		'#default_value' => variable_get('livedata_moonbox_nfq', TRUE)
					);

				$form['moonbox']['phase_settings']['next_last_quarter'] = array(
    		'#type' => 'checkbox',
				'#title' => t('Next last quarter'),
    		'#default_value' => variable_get('livedata_moonbox_nlq', TRUE)
					);
					
    return $form;
}